package com.wnhz.work.yz.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.adapter.ProjectContentAdapter;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.bean.InstallDetailsBean;
import com.wnhz.work.yz.bean.ProjectChildBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.NoScrollListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class InstallDetailsActivity extends BaseActivity implements View.OnClickListener {

    private InstallDetailsBean.InfoEntity info2 = new InstallDetailsBean.InfoEntity();
    private RecyclerView recycler, install, check;
    private TextView order_num, status, creat_time, price;

    private TextView submit;
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Finish1 event) {
        finish();
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);//反注册EventBus
        super.onDestroy();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_install_details4);
      //  MyApplication.getInstance().addDestoryActivity(this, "inssss");
        EventBus.getDefault().register(this);
        info2 = (InstallDetailsBean.InfoEntity) getIntent().getSerializableExtra("info2");
        if (info2 == null) {
            MyToast("订单异常");
            finish();
        }
        order_num = (TextView) findViewById(R.id.order_num);
        order_num.setText("" + info2.getOrder_no());
        status = (TextView) findViewById(R.id.status);
        switch (Integer.parseInt(info2.getStatus())) {
            case 0:
                status.setText("已取消");
                break;
            case 1:
                status.setText("待付款");
                break;
            case 2:
                status.setText("待接单");
                break;
            case 3:
                status.setText("待安装");
                break;
            case 4:
                status.setText("安装中");
                break;
            case 5:
                status.setText("待验收");
                break;
            case 6:
                status.setText("验收完成");
                break;
            case 7:
                status.setText("整改中");
                break;
            case 8:
                status.setText("整改完待验收");
                break;
        }

        TextView area_nums = (TextView) findViewById(R.id.area_nums);
        TextView area = (TextView) findViewById(R.id.area);

        if ("0".equals(info2.getNums())) {
            area_nums.setText("面积：");
            area.setText(info2.getArea() + "㎡");
        } else {
            area_nums.setText("套数：");
            area.setText(info2.getNums() + "套");
        }
        if ("1".equals(info2.getPid()) && "0".equals(info2.getType())) {
            area_nums.setText("面积：");
            area.setText(info2.getArea() + "㎡");
        }
        creat_time = (TextView) findViewById(R.id.creat_time);
        creat_time.setText(info2.getCreate_time());
        price = (TextView) findViewById(R.id.price);
        price.setText("￥" + info2.getAmount() + "元");

        //---
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("订单详情");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        submit = (TextView) findViewById(R.id.submit);

        if ("3".equals(info2.getStatus())) {
            submit.setText("开始安装");
        } else {
            submit.setText("查看进度");
        }

        submit.setVisibility(View.VISIBLE);
        submit.setOnClickListener(this);
        //
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recycler.setAdapter(new BaseRVAdapter(this, info2.getOrderdetail()) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_order;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position2) {
                x.image().bind(holder.getImageView(R.id.img), info2.getOrderdetail().get(position2).getGoods_img());
                holder.setTextView(R.id.title, info2.getOrderdetail().get(position2).getGoods_name());
                holder.setTextView(R.id.type, info2.getOrderdetail().get(position2).getGoods_name());
                holder.setTextView(R.id.area, info2.getOrderdetail().get(position2).getGoods_des());
                holder.setTextView(R.id.num, "x" + info2.getOrderdetail().get(position2).getNums() + "/" + info2.getOrderdetail().get(position2).getUnit());
                holder.getTextView(R.id.price).setVisibility(View.GONE);
                holder.setTextView(R.id.price, "￥" + info2.getOrderdetail().get(position2).getPrice());
            }

        });

        TextView name = (TextView) findViewById(R.id.name);
        name.setText(info2.getRealname());
        TextView tel = (TextView) findViewById(R.id.tel);
        tel.setText(info2.getTel());
        TextView address0 = (TextView) findViewById(R.id.address0);
        address0.setText(info2.getProvince() + "-" + info2.getCity() + "-" + info2.getDistrict());
        TextView address = (TextView) findViewById(R.id.address);
        address.setText(info2.getAddress());
        TextView time = (TextView) findViewById(R.id.time);
        time.setText(info2.getService_time());
        TextView remark = (TextView) findViewById(R.id.remark);
        remark.setText(info2.getRemark());
        if (!"0".equals(info2.getInstall_id())) {
            LinearLayout install_id = (LinearLayout) findViewById(R.id.install_id);
            install_id.setVisibility(View.VISIBLE);
            install = (RecyclerView) findViewById(R.id.install);
            install.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
            install.setAdapter(new BaseRVAdapter(this, info2.getInstall()) {
                @Override
                public int getLayoutId(int viewType) {
                    return R.layout.item_install2;
                }

                @Override
                public void onBind(BaseViewHolder holder, int position) {
                    x.image().bind(holder.getImageView(R.id.install_img), info2.getInstall().get(position).getImg());
                    holder.setTextView(R.id.install_name, info2.getInstall().get(position).getName());
                    holder.setTextView(R.id.install_tel, info2.getInstall().get(position).getTel());
                }


            });
        }

        if (!"0".equals(info2.getChecker_id())) {
            LinearLayout checker_id = (LinearLayout) findViewById(R.id.checker_id);
            checker_id.setVisibility(View.VISIBLE);
//            ImageView cheimg = (ImageView) findViewById(R.id.checker_img);
//            x.image().bind(cheimg, info2.getChecker_img());
//            TextView checker_name = (TextView) findViewById(R.id.checker_name);
//            checker_name.setText(info2.getChecker_name());
//            TextView checker_tel = (TextView) findViewById(R.id.checker_tel);
//            checker_tel.setText(info2.getChecker_tel());
            check = (RecyclerView) findViewById(R.id.check);
            check.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
            check.setAdapter(new BaseRVAdapter(this, info2.getCheck()) {
                @Override
                public int getLayoutId(int viewType) {
                    return R.layout.item_install2;
                }

                @Override
                public void onBind(BaseViewHolder holder, int position) {
                    x.image().bind(holder.getImageView(R.id.install_img), info2.getInstall().get(position).getImg());
                    holder.setTextView(R.id.install_name, info2.getCheck().get(position).getName());
                    holder.setTextView(R.id.install_tel, info2.getCheck().get(position).getTel());
                }


            });
        }

        if (!"0".equals(info2.getBudget_id())) {
            findViewById(R.id.line222).setVisibility(View.GONE);
            LinearLayout buttom44 = (LinearLayout) findViewById(R.id.buttom44);
            buttom44.setVisibility(View.GONE);
            LinearLayout budget_id = (LinearLayout) findViewById(R.id.budget_id);
            budget_id.setVisibility(View.VISIBLE);
            TextView order_time = (TextView) findViewById(R.id.order_time);
            order_time.setText("下单时间：" + info2.getBudget().getOrder_time());
            TextView tv_type = (TextView) findViewById(R.id.tv_type);
            tv_type.setTextColor(0xffe9573d);
            findViewById(R.id.rl_type1).setVisibility(View.GONE);
            findViewById(R.id.rl_type2).setVisibility(View.GONE);

            String str = "项目名称:<font color=\"#e9573d\">" + info2.getBudget().getRecord_name() + "</font>";
            TextView xiangmu_name = (TextView) findViewById(R.id.xiangmu_name);
            xiangmu_name.setText(Html.fromHtml(str));
            TextView tv_project_code = (TextView) findViewById(R.id.tv_project_code);
            String s = "" + info2.getBudget().getBudget_no();
            if ("null".equals(s) || "".equals(s)) {
                s = "暂无合同号";
            }
            tv_project_code.setText("合同号:" + s);
            TextView tv_goods_number = (TextView) findViewById(R.id.tv_goods_number);
            tv_goods_number.setText("共" + info2.getBudget().getCount() + "套商品 合计：¥");
            TextView tv_all_price = (TextView) findViewById(R.id.tv_all_price);
            tv_all_price.setText(info2.getBudget().getTotal_fee());

            List<InstallDetailsBean.InfoEntity.BudgetEntity.DataEntity> data = info2.getBudget().getData();
            for (int i = 0; i < data.size(); i++) {
                ProjectChildBean b = new ProjectChildBean();
                b.setGoods_name(data.get(i).getGoods_name());
                b.setRoom_number(data.get(i).getRoom_number());
                b.setGoods_pic(data.get(i).getGoods_pic());
                b.setPrice(data.get(i).getPrice());
                b.setColor(data.get(i).getColor());
                b.setIf_tepi(data.get(i).getIf_tepi());
                b.setNums(data.get(i).getNums());
                b.setPackage_cate(data.get(i).getPackage_cate());
                b.setSku_info(data.get(i).getSku_info());
                b.setTepi_price(data.get(i).getTepi_price());
                dataList.add(b);
            }


            ProjectContentAdapter mAdapter = new ProjectContentAdapter(this, dataList, info2.getBudget().getBudget_id(), "999", info2.getBudget().getRecord_name());

            NoScrollListView my_listView = (NoScrollListView) findViewById(R.id.my_listView);
            my_listView.setAdapter(mAdapter);
        }

    }

    private List<ProjectChildBean> dataList = new ArrayList<>();

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
                if ("3".equals(info2.getStatus())) {
                    showDialogv7("是否开始安装?", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            upanzhuang();
                        }
                    });
                } else {
                    startActivity(new Intent(InstallDetailsActivity.this, WuLiuListActivity.class)
                            .putExtra("bao", getIntent().getIntExtra("bao", 0))
                            .putExtra("budget_id", getIntent().getStringExtra("budget_id")).putExtra("type", getIntent().getStringExtra("type")));

                }
                break;

        }
    }

    private void upanzhuang() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("orderid", getIntent().getStringExtra("budget_id"));
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----xq" + key, "" + value + "\n");
            }
        }
        showDialog();
        XUtil.Post(Url.DECORATE_STARTDECORATE, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    super.onSuccess(result);
                    if ("1".equals(res)) {
//                        activity.MyToast(info);
                        MyToast(info);
                        info2.setStatus("4");
                        submit.setText("查看进度");
                        status.setText("安装中");
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();

            }
        });

    }
}
